######################################
##########CONJOINT ANALYSIS###########
######################################

#INSTALL CREGG
if (!require("remotes")) {
  install.packages("remotes")
}
remotes::install_github("leeper/cregg")

#LOAD /INSTALL PACKAGES
pacman::p_load(
  cregg, foreign, haven, sjmisc, magrittr, tidyverse, ggpubr, 
  patchwork
)

### SET WORKING DIRECTORY TO FILE LOCATION
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

#LOAD DATA
paper_risp<-read.dta("goodpolitician.dta")

#SET GRAPH LABELS
labamce <- c("strong leader, hinders collegiality", "no strong leader, favour collegiality", "(LEADERSHIP)", 
             "focused on the interests of her/his voters", "focused on the wide public", "(VISION OF ROLE)", 
             "no policy/no english", "policy/no english", "no policy/english", "policy/english", "(COMPETENCE)",
             "under investigation", "clean record", "(INTEGRITY)",
             "distant", "caring", "(SOCIAL SKILLS)",
             "coarse and rude", "proper and refined", "(COMMUNICATION)",
             "politician", "manual worker", "farmer", "manager", "uni professor", "engineer", "(JOB POSITION)",
             "man", "woman", "(GENDER)")

labmm <- c("strong leader, hinder collegiality", "no strong leader, favour collegiality", "(LEADERSHIP)", 
           "focused on the interests of her/his voters", "focused on the wide public", "(VISION OF ROLE)", 
           "no policy/no english", "policy/no english", "no policy/english", "policy/english", "(COMPETENCE)",
           "under investigation", "clean record", "(INTEGRITY)",
           "distant", "caring", "(SOCIAL SKILLS)",
           "coarse and rude", "proper and refined", "(COMMUNICATION)",
           "politician", "manual worker", "farmer", "manager", "uni professor", "engineer", "(JOB POSITION)",
           "man", "woman", "(GENDER)")

#SET REFERENCE CATEGORY
paper_risp$SESSO <- relevel(paper_risp$SESSO, ref=1)
paper_risp$POS<- relevel(paper_risp$POS, ref=6)
paper_risp$STILE <- relevel(paper_risp$STILE, ref=2)
paper_risp$CAPA <- relevel(paper_risp$CAPA, ref=2)
paper_risp$INTE <- relevel(paper_risp$INTE, ref=2)
paper_risp$COMPE <- relevel(paper_risp$COMPE, ref=1)
paper_risp$VISI <- relevel(paper_risp$VISI, ref=2)
paper_risp$LEAD <- relevel(paper_risp$LEAD, ref=1)


#DIAGNOSTICS

#FREQUENCIES
plot(cj_freqs(paper_risp, CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD, id = ~id)) + ggplot2::theme(legend.position = "none") + 
  scale_x_discrete(labels= labamce) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14,face="bold"))

ggsave("figureApp2.png", dpi = 600)

#BALANCE TEST
plot(mm(paper_risp, populism2 ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD, id = ~id), 
     xlim = c(-0.2, 
              0.2), vline = mean(paper_risp$populism2, na.rm = TRUE)) +
  ggplot2::theme(legend.position = "none") + scale_y_discrete(labels= labamce) +
  geom_point(size = 2) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14,face="bold"))

ggsave("figureApp3.png", dpi = 600)


#MM
mm_gp <- CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD
mm_gp_graph <- plot(mm(paper_risp, mm_gp, weights = ~ weight1_istr_w2, id = ~id), vline = 0.5) + ggplot2::theme(legend.position = "none") + 
  scale_y_discrete(labels= labmm) +
  xlab("Effect on the Pr of winning a seat in EUP (MM)") +
  geom_point(size = 2) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14,face="bold"))

ggsave("figureApp9.png", dpi = 600)

#MM vs DIFF

#POPULISM (MEDIAN)
paper_risp$popmedian2  <- factor(paper_risp$popmedian2 , levels=c("0", "1"), labels=c("Low populism", "High populism"))

mm_by_populism <- cj(
  subset(paper_risp, !is.na(popmedian2)),
  CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD,
  weights = ~ weight1_istr_w2, id = ~ id,
  estimate = "mm",
  by = ~ popmedian2
) 

mm_diff_populism <- cj(
  subset(paper_risp, !is.na(popmedian2)),
  CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD,
  weights = ~ weight1_istr_w2, id = ~ id,
  estimate = "mm_diff",
  by = ~ popmedian2
)

plot(rbind(mm_by_populism, mm_diff_populism)) + ggplot2::facet_wrap(~BY, ncol = 3L) +
  ggplot2::theme(legend.position = "none")  + 
  scale_y_discrete(labels= labmm) +
  geom_point(size = 2) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14,face="bold")) +
  theme(strip.text = element_text(size=14))

ggsave("figureApp10.png", dpi = 600)


#MODEL FULL SAMPLE (ARTISAN INCLUDED)
paper_risp_artisan<-read.dta("goodpolitician_artisan.dta")

#SET GRAPH LABELS
labamce <- c("strong leader, hinders collegiality", "no strong leader, favour collegiality", "(LEADERSHIP)", 
             "focused on the interests of her/his voters", "focused on the wide public", "(VISION OF ROLE)", 
             "no policy/no english", "policy/no english", "no policy/english", "policy/english", "(COMPETENCE)",
             "under investigation", "clean record", "(INTEGRITY)",
             "distant", "caring", "(SOCIAL SKILLS)",
             "coarse and rude", "proper and refined", "(COMMUNICATION)",
             "politician", "manual worker", "farmer", "artisan", "manager", "uni professor", "engineer", "(JOB POSITION)",
             "man", "woman", "(GENDER)")

#SET REFERENCE CATEGORY
paper_risp_artisan$SESSO <- relevel(paper_risp_artisan$SESSO, ref=1)
paper_risp_artisan$POS<- relevel(paper_risp_artisan$POS, ref=7)
paper_risp_artisan$STILE <- relevel(paper_risp_artisan$STILE, ref=2)
paper_risp_artisan$CAPA <- relevel(paper_risp_artisan$CAPA, ref=2)
paper_risp_artisan$INTE <- relevel(paper_risp_artisan$INTE, ref=2)
paper_risp_artisan$COMPE <- relevel(paper_risp_artisan$COMPE, ref=1)
paper_risp_artisan$VISI <- relevel(paper_risp_artisan$VISI, ref=2)
paper_risp_artisan$LEAD <- relevel(paper_risp_artisan$LEAD, ref=1)

#AMCE ARTISAN
amces_gp_artisan <- cj(paper_risp_artisan, CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD, 
                       weights = ~ weight1_istr_w2, id = ~id)

amces_gp_graph_artisan <- plot(amces_gp_artisan) + ggplot2::theme(legend.position = "none") + scale_y_discrete(labels= labamce) +
  xlab("Change in the Pr of winning a seat in EUP (AMCE)") +
  geom_point(size = 2) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14,face="bold")) 

ggsave("figureApp11.png", dpi = 600)

#AMCE RATING
paper_risp_rating<-read.dta("goodpolitician_ratingd.dta")

#SET GRAPH LABELS
labamce <- c("strong leader, hinders collegiality", "no strong leader, favour collegiality", "(LEADERSHIP)", 
             "focused on the interests of her/his voters", "focused on the wide public", "(VISION OF ROLE)", 
             "no policy/no english", "policy/no english", "no policy/english", "policy/english", "(COMPETENCE)",
             "under investigation", "clean record", "(INTEGRITY)",
             "distant", "caring", "(SOCIAL SKILLS)",
             "coarse and rude", "proper and refined", "(COMMUNICATION)",
             "politician", "manual worker", "farmer", "manager", "uni professor", "engineer", "(JOB POSITION)",
             "man", "woman", "(GENDER)")

#SET REFERENCE CATEGORY
paper_risp_rating$SESSO <- relevel(paper_risp_rating$SESSO, ref=1)
paper_risp_rating$POS<- relevel(paper_risp_rating$POS, ref=6)
paper_risp_rating$STILE <- relevel(paper_risp_rating$STILE, ref=2)
paper_risp_rating$CAPA <- relevel(paper_risp_rating$CAPA, ref=2)
paper_risp_rating$INTE <- relevel(paper_risp_rating$INTE, ref=2)
paper_risp_rating$COMPE <- relevel(paper_risp_rating$COMPE, ref=1)
paper_risp_rating$VISI <- relevel(paper_risp_rating$VISI, ref=2)
paper_risp_rating$LEAD <- relevel(paper_risp_rating$LEAD, ref=1)

#AMCE
amces_gp_rating <- cj(paper_risp_rating, RATE ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD, 
                      weights = ~ weight1_istr_w2, id = ~id)

amces_gp_graph_rating <- plot(amces_gp_rating) + ggplot2::theme(legend.position = "none") + scale_y_discrete(labels= labamce) +
  xlab("Change in the Pr of winning a seat in EUP (AMCE)")+
  geom_point(size = 2) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14,face="bold")) 

ggsave("figureApp12.png", dpi = 600)

#POPULISM (MEDIAN)
paper_risp$popmedian1  <- factor(paper_risp$popmedian1 , levels=c("0", "1"), labels=c("Low populism", "High populism"))

#AMCE vs DIFF
amce_by_populism <- cj(
  subset(paper_risp, !is.na(popmedian1)),
  CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD,
  weights = ~ weight1_istr_w2, id = ~ id,
  estimate = "amce",
  by = ~ popmedian1
)

amce_diff_populism <- cj(
  subset(paper_risp, !is.na(popmedian1)),
  CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD,
  weights = ~ weight1_istr_w2, id = ~ id,
  estimate = "amce_diff",
  by = ~ popmedian1
)

plot(rbind(amce_by_populism, amce_diff_populism)) + ggplot2::facet_wrap(~BY, ncol = 3L) +
  ggplot2::theme(legend.position = "none") + scale_y_discrete(labels= labamce) +
  geom_point(size = 2) +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=14,face="bold")) +
  theme(strip.text = element_text(size=14))

ggsave("figureApp13.png", dpi = 600)


#ATTENTION CHECKS

#LOAD DATA
paper_risp_attention<-read.dta("goodpolitician_attention.dta")

#SET REFERENCE CATEGORY
paper_risp_attention$SESSO <- relevel(paper_risp_attention$SESSO, ref=1)
paper_risp_attention$POS<- relevel(paper_risp_attention$POS, ref=6)
paper_risp_attention$STILE <- relevel(paper_risp_attention$STILE, ref=2)
paper_risp_attention$CAPA <- relevel(paper_risp_attention$CAPA, ref=2)
paper_risp_attention$INTE <- relevel(paper_risp_attention$INTE, ref=2)
paper_risp_attention$COMPE <- relevel(paper_risp_attention$COMPE, ref=1)
paper_risp_attention$VISI <- relevel(paper_risp_attention$VISI, ref=2)
paper_risp_attention$LEAD <- relevel(paper_risp_attention$LEAD, ref=1)

labamce <- c("strong leader, hinders collegiality", "no strong leader, favour collegiality", "(LEADERSHIP)", 
             "focused on the interests of her/his voters", "focused on the wide public", "(VISION OF ROLE)", 
             "no policy/no english", "policy/no english", "no policy/english", "policy/english", "(COMPETENCE)",
             "under investigation", "clean record", "(INTEGRITY)",
             "distant", "caring", "(SOCIAL SKILLS)",
             "coarse and rude", "proper and refined", "(COMMUNICATION)",
             "politician", "manual worker", "farmer", "manager", "uni professor", "engineer", "(JOB POSITION)",
             "man", "woman", "(GENDER)")

#AMCE
amces_gp <- cj(paper_risp_attention, CHO ~ SESSO + POS + STILE + CAPA + INTE + COMPE + VISI + LEAD, 
               weights = ~ weight1_istr_w2, id = ~id)

amces_gp_graph <- plot(amces_gp) + ggplot2::theme(legend.position = "none") + scale_y_discrete(labels= labamce) +
  xlab("Change in the Pr of winning a seat in EUP (AMCE)") +
  geom_point(size = 2) +
  theme(axis.text=element_text(size=12))

ggsave("figureApp14.png", dpi = 600)

